<?php
declare(strict_types=1);
/**
 * @project: 兔兔找图微信小程序
 * @author: kert
 * @date: 2023/5/16
 * @link: https://www.tutudati.com/
 * @site: 微信搜索-兔兔找图
 */
namespace App\Logic\User\Repository\User;

use App\Exceptions\DbException;
use App\Logic\User\Repository\UserRepositoryInterface;
use App\Models\User\User\AuthorFans;
use Closure;
use Illuminate\Support\Facades\Log;
use Throwable;

class AuthorFansRepository implements UserRepositoryInterface
{

    /**
     * 用户中心关注列表
     * @param Closure $closure
     * @param int $perSize
     * @param array $searchFields
     * @return array
     */
    public function repositorySelect(Closure $closure, int $perSize, array $searchFields = []): array
    {
        $items = AuthorFans::query()
            ->with(["author:qr_url,uid,nickname"])
            ->where($closure)
            ->orderByDesc("id")
            ->paginate($perSize, $searchFields);

        return [
            "items" => $items->items(),
            "page"  => $items->currentPage(),
            "size"  => $perSize,
            "total" => $items->total(),
        ];
    }

    /**
     * 创作者中心关注列表
     * @param Closure $closure
     * @param int $perSize
     * @param array $searchFields
     * @return array
     */
    public function repositoryAuthorSelect(Closure $closure, int $perSize, array $searchFields = []): array
    {
        $items = AuthorFans::query()
            ->with(["user:nickname,avatar_url,uid"])
            ->where($closure)
            ->orderByDesc("id")
            ->paginate($perSize, $searchFields);

        return [
            "items" => $items->items(),
            "page"  => $items->currentPage(),
            "size"  => $perSize,
            "total" => $items->total(),
        ];
    }

    /**
     * @throws DbException
     */
    public function repositoryCreate(array $insertInfo): bool
    {
        try {
            $model = AuthorFans::query()->create($insertInfo);
            return !empty($model->getKey());
        } catch (Throwable $throwable) {
            Log::error("关注作者信息错误" . $throwable->getMessage() . json_encode($insertInfo, JSON_UNESCAPED_UNICODE));
            preg_match("/idx_au/", $throwable->getMessage(), $str);
            if (!empty($str)) {
                throw new DbException("您已关注该作者");
            }
            return false;
        }
    }

    public function repositoryFind(Closure $closure, array $searchFields = []): array
    {
        return [];
    }

    public function repositoryUpdate(array $updateWhere, array $updateInfo): int
    {
        return 0;
    }

    public function repositoryWhereInDelete(array $deleteWhere, string $field): int
    {
        return 0;
    }

    public function repositoryDelete(array $deleteWhere): int
    {
        return AuthorFans::query()->where($deleteWhere)->delete();
    }

    /**
     * 用户关注作者总数
     * @param array $searchWhere
     * @return int
     */
    public function repositoryUserTotal(array $searchWhere): int
    {
        return AuthorFans::query()->where($searchWhere)->count();
    }
}
